package com.qfedu.food.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.food.dao.TCourseDao;
import com.qfedu.food.pojo.TCourse;
import com.qfedu.food.pojo.TUser;
import com.qfedu.food.service.TCourseService;
import com.qfedu.food.util.PageRequest;
import com.qfedu.food.util.PageResult;
import com.qfedu.food.util.PageUtils;
import com.qfedu.food.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @auther BUG小能手
 * @date 2021/1/11 20:23
 */
@Service
public class TCourseServiceImpl implements TCourseService {

    @Autowired
    private TCourseDao tCourseDao;

    @Override
    public R queryAll() {
        return R.ok(tCourseDao.serviceAll());
    }

    @Override
    public R findAll(int type) {
        TUser tUser = new TUser();
        if (tUser == null) {
            return R.fail();
        } else {
            if (tUser.getType() == 1) {
                return R.ok(tCourseDao.serviceAll());
            } else {
                return R.ok(tCourseDao.findNameAndDecs());
            }
        }
    }



    @Override
    public PageResult findPage(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    /**
     * 调用分页插件完成分页
     * @param pageRequest
     * @return
     */
    private PageInfo<TCourse> getPageInfo(PageRequest pageRequest) {

        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<TCourse> tCourses = tCourseDao.selectPage();
        return new PageInfo<>(tCourses);
    }



    /**
     * 通过用户id查询用户状态是否为会员从而展示不同的内容*/
    @Override
    public R findTypeById(Long id) {
        if (id != 0) {
            TUser user = tCourseDao.findTypeById(id);
            if (user==null){
                return R.fail();
            }
            if(user.getType()==1){
                return R.ok(tCourseDao.serviceAll());
            } else {
                return R.ok(tCourseDao.findNameAndDecs());
            }
        }
        return R.fail();
    }


}
